<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
use App\Http\Requests\HomeLoginPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class QianLoginController extends Controller
{
    //显示前台登录页面

    public function getLogin(){
        //显示模板
        // echo 22;

         $peizhi = DB::table('webcon')->first(); 
         session(['peizhi'=>$peizhi]);
         return view('home.login.login');

        $peizhi = DB::table('webcon')->first();
        session(['peizhi'=>$peizhi]);
        // 获取之前的url
        $res = $_SERVER['HTTP_REFERER']; 
        session(['url'=>$res]);
        return view('home.login.login');

    }
     //前台登陆验证
   

     public function postHlogin(Request $request){
        //显示模板
       // echo 22;
         $peizhi = DB::table('webcon')->first();
        session(['peizhi'=>$peizhi]);

      
          //dd(session('peizhi'));
         // return view('home.login.login');
         //dd($request->all());
        //检测身份
        // $user = DB::table('user')->get();
        // dd($user);
        //检测用户是否存在
         $user = DB::table('user')->where('username',$request->input('username'))->first();
         if(!$user){
            return back()->with('error','用户名或密码错误');
         }
        //dd($user);

         //检测密码是否一致
         if(Hash::check($request->input('password'),$user->password)){
            //成功
            session(['id'=>$user->id]);
            session(['username'=>$user->username]);
            session(['pic'=>$user->pic]);



            //跳转到前台首页
            return redirect('/home/index')->with('success','欢迎'.$user->username.'进入前台');

            if(!session('url')){
              $res = $_SERVER['HTTP_REFERER']; 
              session(['url'=>$res]);
              $url=session('url');
            }
            $url=session('url');
            // dd($url);
            //跳转到后台首页
            return redirect($url)->with('success','欢迎'.$user->username.'进入前台');


            //跳转到后台首页
            return redirect('/home/index')->with('success','欢迎'.$user->username.'进入后台');

         }else{
            //失败
            return back()->with('error','用户名或密码错误');
         }
    }


    //注册

      public function getZhulogin(){
        //显示模板
        //echo 22;die;
         return view('home.zhuce.zhuce');
    }

    public function postDologin(Request $request)

    {
      
        //dd($request->all());
        // $v = session('Vcode');
        // dd($v);

        //检测验证码是否正确
        if($request->input('vcode') != session('Vcode')){
            return back()->with('error','验证码不正确');
        }
      

        $this->validate($request, [
        'username' => 'required|max:255',
        'email' => 'required',
        'password' => 'required',
        
    ],[
         'username.required'=>'用户名不能为空',
         'username.required'=>'邮箱不能为空',
         'password.required'=>'密码不能为空', 
        
    ]);

        

        //提取部分数据
            $data = $request->only(['username','email','password']);
        
             $data['password'] = Hash::make($data['password']);
            $data['token']=str_random(50);

             // dd($data);
        //执行数据库插入操作

             $id = DB::table('user')->insertGetId($data);
             //var_dump($id);die;
             if($id){
                //注册成功 发送邮件
                //echo $id;
                $token = $data['token'];

                $this->sendMail($id,$token);

                return redirect('/home/index');
             }else{
                return back()->with('error','注册失败');

             }
            // dd($res);
            //判断是否添加成功
     
    }
   
    //前台退出

    public function getLoginout(){
        // echo '222';
         session()->forget('id');
        

         session()->forget('username');

        return redirect('/home/index');
        // return black();
    }

    //验证码
    public function getVcode()
    {
        ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();

       //看到图片输出 需要die
        // die;
    }
        //右键发送测试
    public function sendMail($id,$token)
    {
        //dd($id,$token);

      //   Mail::raw('<a href="http://mao.cn/home/jihuo?id=1&token=2222">点击激活账号</a>', function ($message) {
      //   //设置邮件标题
      //   $message->subject('激活邮件');
      //   //发送给谁
      //   $message->to('1157677047@qq.com');
      // });

        //$id=1;
        //$token=222;
        $email='1157677047@qq.com';
         Mail::send('/home/mail.re', ['id'=>$id,'token'=>$token], function ($message)use($email) {
            $message->to($email)->subject('激活邮件');
        });
        // echo 'jaijie';
       
    }
// 激活用户的方法

    public function getJihuo(Request $request){
        // dd($request->all());
            $id = $request->input('id');

        //根据id查询用户
            $user = DB::table('user')->where('id',$id)->first();

        //请求是否正确
            if($user->token==$request->input('token')){
                $data['token'] = str_random(50);
                DB::table('user')->where('id',$id)->update($data);
                return redirect('/home/login/login');
            
            }else{
                echo '失败';
            }
    }


    //找回密码

    public function getZhaohui(){
        //显示页面
        return view('home.login.zhaohui');
    }
        //处理页面
    public function postDozhao(Request $request){

     // dd($request->all());
        $v = session('Vcode');
        // dd($v);
      // dd ($request->input('email'));
        $data = DB::table('user')->where('email',$request->input('email'))->first();
        // dd($data);

        if(!empty($data)){
            $id=$data->id;
            $email=$data->email;
            $token=$data->token;
            //dd($email);
            //define('Mail',$email);
            //echo Mail;die;
            //成功发送邮件
        
         Mail::raw('找回密码请点击进行激活 http://mao.cn/home/login/xm?email='.$email, function ($message) {
               //设置邮件标题
                 $message->subject('激活邮件');
                //发送给谁
                $message->to('1157677047@qq.com');
            });
         return view('home.login.zhaohui',['email'=>$email]);
        }else{
            return back()->with('error','账户邮箱不存在');
        }
    }

        //设置新密码
    
    public function getXm(Request $request){

        //$email = $request->input('email');
       // dd($email);
        $data = DB::table('user')->where('email',$request->input('email'))->first();
        //dd($data);
        $username = $data->username;
        $id = $data->id;
        // dd($data);
        return view('home.login.xinmima',['username'=>$username,'id'=>$id]);
    }

    
    //执行设置新密码
    
    public function postDoxinmima(Request $request){
        $password = Hash::make($request->input('password'));
        $id = $request->input('id');
        //执行修改
        $res = DB::table('user')->where('id',$id)->update(['password'=>$password]);
        if($res){
            // echo '修改成功';
            return redirect('/home/login/login');
        }else{
            echo '修改失败';
        }
     }
  }


